package boardgamer.common.client;

import android.graphics.Canvas;
import android.graphics.RectF;
import boardgamer.common.client.layout.LayoutGroup;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Button that can be set to be enabled / disabled
 *
 * @author garysoed
 */
public class EnabledButton implements Button {

  private final Button enabledButton;
  private final Display disabledButton;
  private final LayoutGroup group;
  private boolean isEnabled;

  public EnabledButton(Button enabledButton, Display disabledButton, boolean isEnabled) {
    this.enabledButton = checkNotNull(enabledButton, "enabledButton");
    this.disabledButton = checkNotNull(disabledButton, "disabledButton");
    this.group = new LayoutGroup()
        .add(enabledButton).atCurrentRect()
        .add(disabledButton).atCurrentRect();
    this.isEnabled = isEnabled;
  }

  public void setEnabled(boolean isEnabled) {
    this.isEnabled = isEnabled;
  }

  public boolean isEnabled() {
    return isEnabled;
  }

  @Override
  public void draw(Canvas canvas) {
    if (isEnabled) {
      enabledButton.draw(canvas);
    } else {
      disabledButton.draw(canvas);
    }
  }

  @Override
  public void handleEvent(float eventX, float eventY, int eventAction) {
    if (isEnabled) {
      enabledButton.handleEvent(eventX, eventY, eventAction);
    }
  }

  @Override
  public RectF getCollisionBox() {
    return enabledButton.getCollisionBox();
  }

  @Override
  public void setBoundingRect(RectF boundingRect) {
    group.setBoundingRect(boundingRect);
  }

  @Override
  public RectF getBoundingRect() {
    return group.getBoundingRect();
  }
}
